#include "base_memory.h"

#include "base_log.h"


//-----------------------------------------------------------------------------
// Memory allocation
//-----------------------------------------------------------------------------


static int _nb_alloc;

void* mem_allocate(size_t size) //Allocate memory. + incremente test memoire
{
    void *test=calloc(size, 1);
    if(test==NULL)
    {
        log_puts(LOG_ERR, "Ran out of memory!");
        exit(EXIT_FAILURE);
    }

    else
    {
        _nb_alloc++;
        return test;
    }
}

void mem_free(void *ptr)// Free memory. + decremente test memoire
{
    _nb_alloc--;
    free(ptr);
}

char* mem_strdup(const char *str) //Duplicate a string using mem_allocate().
{
    int length=strlen(str)+1;
    char *dpd;
    dpd=(char*)mem_allocate(length*sizeof(char));
    strcpy(dpd,str);
    return dpd;
}

int mem_get_nb_allocations(void)
{
    return _nb_alloc;
}
